package org.hyl.javabase.文件;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrCharset {

	public static void main(String[] arg) {
		StrCharset d = new StrCharset();

		d.fun2();
		// fun4();
	}

	public void fun(String d1) {
		try {
			System.out.println(d1.length());
		} catch (java.lang.NullPointerException e) {
			System.out.println("呵呵 我知道会发生这个异常1");
		}
	}

	public void fun0(String d1) throws java.lang.NullPointerException {

		System.out.println(d1.length());

	}

	// byte[] -> String ;String-->byte[]
	public void fun1() {

		String s = "你好";
		try {
			byte[] unicodebytes = s.getBytes();
			System.out.println("unicode:" + bytesToHexString(unicodebytes));
			byte[] iso8859bytes = s.getBytes("ISO-8859-1");
			System.out.println("iso8859:" + bytesToHexString(iso8859bytes));
			byte[] utfbytes = s.getBytes("UTF8");
			System.out.println("utf8:" + bytesToHexString(utfbytes));
			byte[] utf8bytes = s.getBytes("UTF-8");
			System.out.println("utf8:" + bytesToHexString(utf8bytes));
			byte[] gbkbytes = s.getBytes("gbk");
			System.out.println("gbk:" + bytesToHexString(gbkbytes));
			byte[] gb2312 = s.getBytes("gb2312");
			System.out.println("gb2312:" + bytesToHexString(gb2312));
			String s1 = null;
			// s1=new String(gb2312,0,gb2312.length,"utf8");
			// s1=new String(utfbytes,0,utfbytes.length,"gbk");
			s1 = new String(iso8859bytes, 0, iso8859bytes.length, "utf-8");
			// s1=new String(gbkbytes,0,gbkbytes.length,"GBK");
			// s1 = new String(gbkbytes);
			System.out.println(s1);

			// 结果 gbk，gb2312，unicode 用utf8 解析 会出现？,反之 出现是乱码

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF; // 负数转 无符号
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	// file =byte[]
	public void fun2() {
		String path = System.getProperty("user.dir") + "/src/javabase/test.txt";// 指定文件
		try {
			FileInputStream in = new FileInputStream(path); // 读取文件路径 默认读取ansi//
															// 编码
			byte bs[] = new byte[in.available()];
			in.read(bs);
			in.close();
			String s1 = new String(bs, 0, bs.length, "iso8859-1");
			System.out.println(unicodeToString(s1));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String unicodeToString(String str) {
		Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
		Matcher matcher = pattern.matcher(str);
		char ch;
		while (matcher.find()) {
			ch = (char) Integer.parseInt(matcher.group(2), 16);
			str = str.replace(matcher.group(1), ch + "");
		}
		return str;
	}



	 //1.string转unicode

	  public static String string2Unicode(String string) {
	         StringBuffer unicode = new StringBuffer();
	         for (int i = 0; i < string.length(); i++) {
	             // 取出每一个字符
	             char c = string.charAt(i);
	             // 转换为unicode
	             unicode.append("\\u" + Integer.toHexString(c));
	         }
	         return unicode.toString();
	     }

	  

	 //2.unicode转string

	 public static String unicode2String(String unicode) {
	         StringBuffer string = new StringBuffer();
	         String[] hex = unicode.split("\\\\u");
	         for (int i = 1; i < hex.length; i++) {
	             // 转换出每一个代码点
	             int data = Integer.parseInt(hex[i], 16);
	             // 追加成string
	             string.append((char) data);
	         }
	         return string.toString();
	     }

	public void fun3() throws IOException {
		String path = System.getProperty("user.dir") + "/src/javabase/test.txt";
		File f = new File(path);
		FileInputStream fip;
		fip = new FileInputStream(f);
		// 构建FileInputStream对象
		InputStreamReader reader = new InputStreamReader(fip, "ISo8859-1");
		// 构建InputStreamReader对象,编码与写入相同
		StringBuilder sb = new StringBuilder();
		while (reader.ready()) {
			sb.append((char) reader.read());// 转成char加到StringBuffer对象中
		}
		System.out.println(sb.toString());
		reader.close();
		// 关闭读取流

		fip.close(); // 关闭输入流,释放系统资源

	}

	public static void fun4() {
		String[] input = { "### \u8bbe\u7f6eLogger\u8f93\u51fa\u7ea7\u522b\u548c\u8f93\u51fa\u76ee\u7684\u5730 ###",
				"### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u63a7\u5236\u53f0 ###",
				"### \u628a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u6587\u4ef6\uff1aheros.log ###",
				"### \u663e\u793aSQL\u8bed\u53e5\u90e8\u5206" };
		for (int i = 0; i < input.length; i++) {
			change(input[i]);
		}
	}

	/** 
	 *  
	 */
	private static void change(String input) {
		// TODO Auto-generated method stub
		System.out.println("转换前得结果：" + input);
		try {
			String output = new String(input.getBytes("iso-8859-1"), "utf-8");
			System.out.println("转换后得结果：" + output);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
